package com.journey.org.ui.web

import android.app.Application
import android.content.Intent
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import com.mvvmhabit.org.base.BaseViewModel
import com.mvvmhabit.org.binding.command.BindingAction
import com.mvvmhabit.org.binding.command.BindingCommand
import com.mvvmhabit.org.custom.LoadingLayout

class WebViewModel(application: Application) : BaseViewModel(application) {

    val requestStateField = ObservableInt(LoadingLayout.NETWORK_LOADING)

    override fun onCreate() {
        super.onCreate()
        showLoadDialog()
    }

    val onClickAgainLoadCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {

        }
    })

    val onLoadUrlField = ObservableField<String>("http://www.baidu.com")

    val onToolbarTitleField = ObservableField<String>("欢迎来来到百度")

    val onRefreshWebCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {

        }
    })

    val onWebLoadFinishCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            dismissLoadDialog()
            requestStateField.set(LoadingLayout.HIDE_LAYOUT)
        }
    })

    val onWebLoadErrorCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
//            requestStateField.set(LoadingLayout.NETWORK_ERROR)
        }
    })


    val onFinishCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            finish()
        }
    })

    // 分享
    val onShareCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            share()
        }
    })

    private fun share() {
        var intentShare = Intent()
        intentShare.action = Intent.ACTION_SEND
        intentShare.type = "text/plain"
        intentShare.putExtra(Intent.EXTRA_SUBJECT, onToolbarTitleField.get())
        intentShare.putExtra(Intent.EXTRA_TEXT, onLoadUrlField.get())
        intentShare = Intent.createChooser(intentShare, onToolbarTitleField.get())
        startActivity(intentShare)
    }
}